//
//  GuideHomeCollectionViewCell.m
//  BeautifulShoppings
//
//  Created by GivenBmli on 16/9/13.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "GuideHomeCollectionViewCell.h"
#import "GuideHomeTableViewCell.h"
#import "GuideMiddle.h"
#import "NetworkHandler.h"
#import "GuideHomeModel.h"
#import "GuideHomeCollectionViewCell.h"
#import "GuideHomeViewController.h"
#import "GuidePlayPicture.h"
#import "ToolModel.h"
#import "ToolDB.h"
#import "MJRefresh.h"
#import "TAMacro.h"
@interface GuideHomeCollectionViewCell ()<PlayPictureDelegate>
@end
static NSInteger count = 21;

@implementation GuideHomeCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatTabView:frame];
        [self mjRefresh];
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self.tableView.mj_header beginRefreshing];
            [self mjRefresh];
        }];
        self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
            [self.tableView.mj_footer beginRefreshing];
            [self mjRefreshAdd];
            [self.tableView.mj_footer endRefreshing];
        }];
    }
    return self;
}
- (NSMutableArray *)array {
    if (!_array) {
        self.array = [NSMutableArray array];
    }
    return _array;
}
- (void)creatTabView:(CGRect)frame {
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height-108) style:0];
    self.tableView.tableHeaderView = [[UITableViewHeaderFooterView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 2.5)];
    [self.tableView jxl_setDayMode:^(UIView *view) {
        self.tableView.backgroundColor = [UIColor whiteColor];
    } nightMode:^(UIView *view) {
        self.tableView.backgroundColor = [UIColor grayColor];
    }];
    GuidePlayPicture *guidePlayPicture =[[GuidePlayPicture alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT / 15, SCREEN_WIDTH, self.frame.size.height )];
    [self.tableView.tableHeaderView addSubview:guidePlayPicture];
    guidePlayPicture.delegate = self;
       [self.tableView.tableHeaderView addSubview:self.guidemiddleView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.contentView addSubview:self.tableView];
}
- (void)mjRefresh {
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    if (manager.isReachableViaWWAN == YES && manager.isReachableViaWiFi == NO) {
        [ToolDB createTableS];
        NSString *sql = @"SELECT title,cover_image_url,Id FROM t_ACE;";
        NSMutableArray *array = [ToolDB selectFromSql:sql];
        [array removeObjectAtIndex:0];
        self.array = [NSMutableArray arrayWithArray:array];
        [self.tableView reloadData];
    }else{
//        送女票
        [NetworkHandler getDataByURLString:@"http://api.liwushuo.com/v2/channels/101/items?ad=2&gender=1&generation=2&limit=20&offset=1" BodyString:nil WithDataBlock:^(id result) {
            NSDictionary *dic = [result objectForKey:@"data"];
            NSMutableArray *arr = [dic objectForKey:@"items"];
            for (NSDictionary *dic in arr) {
                GuideHomeModel *model = [GuideHomeModel modelWithDic:dic];
                NSString *sql = [NSString stringWithFormat:@"DELETE FROM t_ACE where title = '%@' and cover_image_url = '%@' and Id = '%ld';", model.title,model.cover_image_url,(long)model.Id];
                [ToolDB dele:sql];
                [ToolDB createTableS];
                NSString *sql2 = [NSString stringWithFormat:@"INSERT INTO t_ACE(title,cover_image_url,ID) VALUES('%@','%@',%ld);", model.title,model.cover_image_url,(long)model.Id];
                [ToolDB execSqlString:sql2];
                [self.array addObject:model];
            }
            [self.tableView reloadData];
            [self.tableView.mj_header endRefreshing];
        }];
    }
}
- (void)mjRefreshAdd {
    [NetworkHandler getDataByURLString:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/channels/101/items?ad=2&gender=1&generation=2&limit=20&offset=%ld", (long)count++] BodyString:nil WithDataBlock:^(id result) {
        NSDictionary *dic = [result objectForKey:@"data"];
        NSMutableArray *arr = [dic objectForKey:@"items"];
        for (NSDictionary *dic in arr) {
            GuideHomeModel *model = [GuideHomeModel modelWithDic:dic];
            [self.array addObject:model];
        }
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
    }];
    [self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return SCREEN_WIDTH / 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    GuideHomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell  == nil) {
        cell = [[GuideHomeTableViewCell alloc]initWithStyle:0 reuseIdentifier:ID];
    }
    [cell jxl_setDayMode:^(UIView *view) {
        cell.backgroundColor = [UIColor whiteColor];
    } nightMode:^(UIView *view) {
        cell.backgroundColor = [UIColor grayColor];
    }];
    GuideHomeModel *model = self.array[indexPath.item];
    cell.guideHomeModel = model;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    GuideHomeModel *model = self.array[indexPath.item];
    self.str = [NSString stringWithFormat:@"%ld",(long)model.Id];
    self.STR = model.title;
    self.image = model.cover_image_url;
    [self.delegate didClickSelectItemAtIndexPathGuideHome:indexPath str:self.str STR:self.STR Image:self.image];
}
- (void)didClickSelectItemAtIndexPathPlayPicture:(NSIndexPath *)indexPath str:(NSString *)str {
    [self.delegates didClickSelectItemAtIndexPathGuideHomePlayPicture:indexPath str:str];
}

@end
